﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0912 //2011.05.16 //Begin
namespace InfoJetSoft.Service.XPath
{
    class BindingTestData
    {
        private static string XdUri = "http://schemas.microsoft.com/office/infopath/2003";

        public XmlDocument Main { get; set; }
        public Dictionary<string, XmlDocument> Sources { get; set; }

        public BindingTestData( Manifest manifest )
        {
            CABFile cabFile = CABFileFinder.findCABFile(manifest, manifest.SampleDataFile);
            XmlDocument testDoc = DOMUtil.buildDoc(cabFile.ByteOutput);
            this.Sources = ParseDataSources(testDoc);
            this.Main = this.Sources[""];
        }

        private static Dictionary<string, XmlDocument> ParseDataSources( XmlDocument testDoc )
        {
            Dictionary<string, XmlDocument> sources = new Dictionary<string, XmlDocument>();

            XmlElement mainElement = testDoc.DocumentElement;

            RemoveSchemaInfo(mainElement);

            List<XmlElement> dataConnElements = CollectDataConnections(mainElement);
            foreach (XmlElement dataConnElement in dataConnElements)
            {
                foreach (XmlNode subNode in dataConnElement.ChildNodes)
                {
                    if (subNode is XmlElement)
                    {
                        XmlElement dataElement = subNode as XmlElement;
                        RemoveSchemaInfo(dataElement);
                        string dataName = dataConnElement.GetAttribute("Name");
                        if (dataName != null && dataName.Length > 0)
                        {
                            XmlDocument dataDoc = new XmlDocument();
                            XmlElement dataDocElement = dataDoc.ImportNode(dataElement, true) as XmlElement;
                            dataDoc.AppendChild(dataDocElement);
                            //CID 0961 //2011.06.21 //Begin
                            if (sources.ContainsKey(dataName))
                            {
                                sources.Remove(dataName);
                            }
                            //CID 0961 //2011.06.21 //End
                            sources.Add(dataName, dataDoc);
                        }
                        break;
                    }
                }
            }

            foreach (XmlElement dataConnElement in dataConnElements)
            {
                XmlNode node = mainElement.RemoveChild(dataConnElement);
            }

            XmlDocument mainDoc = new XmlDocument();
            XmlElement mainDocElement = mainDoc.ImportNode(mainElement, true) as XmlElement;
            mainDoc.AppendChild(mainDocElement);
            sources.Add("", mainDoc);

            return sources;
        }

        private static void RemoveSchemaInfo(XmlElement element)
        {
            XmlElement schemaInfoElement = null;
            foreach (XmlNode childNode in element.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement childElement = childNode as XmlElement;
                    if (childElement.NamespaceURI.Equals(XdUri) && childElement.LocalName.Equals("SchemaInfo"))
                    {
                        schemaInfoElement = childElement;
                        break;
                    }
                }
            }
            if (schemaInfoElement != null)
            {
                element.RemoveChild(schemaInfoElement);
            }
        }

        private static List<XmlElement> CollectDataConnections(XmlElement element)
        {
            List<XmlElement> dataConnections = new List<XmlElement>();
            foreach (XmlNode childNode in element.ChildNodes)
            {
                if (childNode is XmlElement)
                {
                    XmlElement childElement = childNode as XmlElement;
                    if (childElement.NamespaceURI.Equals(XdUri) && childElement.LocalName.Equals("DataConnection"))
                    {
                        dataConnections.Add(childElement);
                    }
                }
            }
            return dataConnections;
        }
    }
}
//CID 0912 //2011.05.16 //End
